<template>
  <div class="app-container table-app-container">
    <div class="fixed-table">
      <ActionBar ref="actionBar" :for-selection="forSelection" />
      <AssetTable
        :loading="loading"
        :columns="insideColumns"
        :data="insideTableData"
        :total="totalElements"
        :page="currentPage"
        :page-size="pageSize"
        :table-height="tableHeight"
        :multiple-selected="false"
        class="fixed-table-table"
        :show-advanced-search="!forSelection"
        @page-change="onPageChange"
        @sort-change="onSortChange"
        @open-drawer="onOpenDrawer"
        @close-filter-item="onCloseFilterItem"
        @selection-change="onSelectionChange"
      >
        <template v-for="column in insideColumns">
          <el-table-column v-if="column.display" :key="column.key" :label="column.title" :prop="column.key" :width="column.width" :sortable="column.sortable" :fixed="column.fixed">
            <template slot-scope="scope">
              <column-render :render="column.render" :row="scope.row" :column="column" />
            </template>
          </el-table-column>
        </template>
        <!-- <el-table-column label="操作" align="center" width="150px" class-name="small-padding fixed-width" fixed="right">
          <template slot-scope="{row}">
            <el-button type="text" title="查看" size="mini" icon="el-icon-view" @click="view(row.id)" />
          </template>
        </el-table-column> -->
      </AssetTable>
    </div>
    <ViewDialog v-if="dialogViewVisible" custom-class="second-dialog-height" title="查看" :append-to-body="true" :show.sync="dialogViewVisible" width="90%" @on-cancel="viewCancel">
      <ViewForm :id="editId" />
    </ViewDialog>
    <ViewDialog v-if="dialogViewRuleVisible" custom-class="second-dialog-height" title="维护说明" :append-to-body="true" :show.sync="dialogViewRuleVisible" width="60%" @on-cancel="viewRuleCancel">
      <ViewRuleForm :id="ruleId" ref="ViewRuleForm" :task-id="taskId" />
    </ViewDialog>
    <FormDialog
      ref="filterDialog"
      custom-class="second-dialog-height"
      :drawer="true"
      width="40%"
      title="筛选"
      cancel-text="重置"
      confirm-text="筛选"
      :show.sync="drawerFormVisible"
      @on-ok="onSearch"
      @on-cancel="onReset"
    >
      <table-filter ref="filterForm" :filter="filter" />
    </FormDialog>
  </div>
</template>

<script>
import ViewDialog from '@neu/biz-core/components/ViewDialog'
import FormDialog from '@neu/biz-core/components/FormDialog'
import ViewRuleForm from './components/viewRuleForm'
import ViewForm from './components/viewForm'
import TableFilter from './components/tableFilter'
import ActionBar from './components/actionBar'
// import PageableTable from '@neu/biz-core/components/PageableTable'
import AssetTable from '@neu/biz-core/components/AssetTable'
import databind from '@neu/biz-core/mixins/databind'
import { formatDate } from '@neu/biz-core/utils/utils'
import {
  page,
  remove
} from '@neu/dcom-ui/api/maintainTask'

const formItemView = {

}
export default {
  name: 'MaintainTaskQuery',
  components: {
    TableFilter,
    ActionBar,
    // PageableTable,
    ViewDialog,
    ViewRuleForm,
    ViewForm,
    FormDialog,
    AssetTable
  },
  mixins: [databind],
  data() {
    return {
      r: page,
      d: remove,
      formItemView: {
        ...formItemView
      },
      insideColumns: [
        {
          title: '任务编码',
          key: 'code',
          render: (h, params) => { // 查看功能
            return h('span', [
              h('el-link', {
                class: 'font-12',
                attrs: { title: params.row.code },
                props: { type: 'primary' },
                on: { click: () => { this.view(params.row.id) } }},
              params.row.code)
            ])
          },
          display: true
        },
        {
          title: '任务名称',
          key: 'name',
          // width: 150,
          // render: (h, params) => { // 查看功能
          //   return h('span', [
          //     h('el-link', {
          //       class: 'font-12',
          //       attrs: { title: params.row.name },
          //       props: { type: 'primary' },
          //       on: { click: () => { this.view(params.row.id) } }},
          //     params.row.name)
          //   ])
          // },
          display: true
        },
        {
          title: '计划名称',
          key: 'planName',
          display: false
        },
        {
          title: '任务来源',
          key: 'source',
          render: (h, params) => {
            const text = params.row.source === 'period' ? '周期任务' : '临时任务'
            return h('span', [
              h('el-tag', text)
            ])
          },
          display: true
        },
        {
          title: '维护类别',
          key: 'modelName',
          display: true
        },
        {
          title: '维护对象',
          key: 'objName',
          display: true
        },
        {
          title: '维护部门',
          key: 'orgName',
          display: true
        },
        {
          title: '维护规则',
          key: 'ruleName',
          render: (h, params) => { // 查看功能
            return h('span', [
              h('el-link', {
                class: 'font-12',
                attrs: { title: params.row.ruleName },
                props: { type: 'primary' },
                on: { click: () => { this.viewRule(params.row.ruleId, params.row.id) } }},
              params.row.ruleName)
            ])
          },
          display: true
        },
        {
          title: '执行人',
          key: 'chargeUserName',
          display: true
        },
        {
          title: '状态',
          key: 'status',
          render: (h, params) => {
            var text = ''
            if (params.row.status === 'undo') {
              text = '未执行'
            }
            if (params.row.status === 'doing') {
              text = '执行中'
            }
            if (params.row.status === 'done') {
              text = '已执行'
            }
            return h('span', [
              h('el-tag', text)
            ])
          },
          display: true
        },
        {
          title: '是否停止服务',
          key: 'suspendService',
          render: (h, params) => {
            const text = params.row.suspendService === true ? '是' : '否'
            return h('span', [
              h('el-tag', text)
            ])
          },
          display: false
        },
        {
          title: '停止服务通知发送状态',
          key: 'noticeSendStatus',
          render: (h, params) => {
            const text = params.row.noticeSendStatus === 'send' ? '已发送' : '未发送'
            return h('span', [
              h('el-tag', text)
            ])
          },
          display: false
        },
        {
          title: '量化时间(小时)',
          key: 'quantifyingTime',
          display: false
        },
        {
          title: '开始时间',
          key: 'startDateTime',
          render: (h, params) => { return h('span', formatDate(params.row.startDateTime)) },
          display: true
        },
        {
          title: '结束时间',
          key: 'endDateTime',
          render: (h, params) => { return h('span', formatDate(params.row.endDateTime)) },
          display: true
        },
        {
          title: '创建时间',
          key: 'createTime',
          render: (h, params) => { return h('span', formatDate(params.row.createTime)) },
          display: false
        }
      ],
      dialogViewVisible: false,
      dialogViewRuleVisible: false,
      drawerFormVisible: false,
      ruleId: -1,
      taskId: -1
    }
  },
  methods: {
    view(id) {
      this.editId = id
      this.dialogViewVisible = true
    },
    viewCancel() {
      this.editId = -1
      this.dialogViewVisible = false
    },
    viewRule(ruleId, id) {
      this.ruleId = ruleId
      this.taskId = id

      if (this.taskId === -1 || this.ruleId === -1 || this.ruleId === null || this.ruleId === 0) {
        return this.$message({
          showClose: true,
          message: '当前任务没有匹配维护规则',
          type: 'warning'
        })
      }

      this.dialogViewRuleVisible = true
    },
    viewRuleCancel() {
      this.ruleId = -1
      this.taskId = -1
      this.dialogViewRuleVisible = false
    },
    onOpenDrawer() { // 筛选-打开抽屉
      this.drawerFormVisible = true
      this.$refs.actionBar.clearForm()
    },
    onCloseFilterItem(item) {
      this.$refs.filterForm.onFilterItemClose(item)
    },
    onSearch() { // 筛选-筛选
      this.formItemView = this.$refs.filterForm.formItem
      this.$refs.filterForm.search()
      this.drawerFormVisible = false
    },
    onReset() { // 筛选-重置
      this.$refs.filterForm.reset()
      this.drawerFormVisible = false
    }
  }
}
</script>
